/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.skatiger.jrove.tag;

import org.skatiger.jrove.JroveContext;
import org.skatiger.jrove.MetaRule;
import org.skatiger.jrove.Metadata;
import org.skatiger.jrove.component.UIComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ComponentRule extends MetaRule {

    final class LiteralAttributeMetadata extends Metadata {

        private final String name;
        private final String value;

        public LiteralAttributeMetadata(String name, String value) {
            this.value = value;
            this.name = name;
        }

        @Override
        public void applyMetadata(JroveContext ctx, Object instance) {
            ((UIComponent) instance).getAttributes().put(this.name, this.value);
        }
    }

    final static class ValueExpressionMetadata extends Metadata {

        private final String name;

        private final TagAttribute attr;

        private final Class<?> type;

        public ValueExpressionMetadata(String name, Class<?> type, TagAttribute attr) {
            this.name = name;
            this.attr = attr;
            this.type = type;
        }

        @Override
        public void applyMetadata(JroveContext ctx, Object instance) {
            ((UIComponent) instance).setValueExpression(this.name, this.attr.getValueExpression(ctx, this.type));
        }

    }

    private final static Logger logger = LoggerFactory.getLogger(ComponentRule.class);

    public final static ComponentRule INSTANCE = new ComponentRule();

    private static void warnAttr(TagAttribute attr, Class<?> type, String n) {
        logger.debug(attr + " Property '" + n + "' is not on type: " + type.getName());
    }

    public ComponentRule() {
        super();
    }

    @Override
    public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta) {
        if (meta.isTargetInstanceOf(UIComponent.class)) {

            // if component and dynamic, then must set expression
            if (!attribute.isLiteral()) {
                Class<?> type = meta.getPropertyType(name);
                if (type == null) {
                    type = Object.class;
                }
                return new ValueExpressionMetadata(name, type, attribute);
            } else if (meta.getWriteMethod(name) == null) {
                // this was an attribute literal, but not property
                warnAttr(attribute, meta.getTargetClass(), name);
                return new LiteralAttributeMetadata(name, attribute.getValue());
            }
        }
        return null;
    }

}
